package com.chinamobile.push

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.KeyEvent
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.chinamobile.push.config.PassPath
import com.cmcc.passenger.ui.base.BasicActivity
import com.chinamobile.push.databinding.ActivityTestBinding
import com.cmcc.shared.IAppLogicHandler
import com.cmcc.shared.SdkManager
import com.cmcc.shared.config.ARouterPath
import com.cmcc.shared.utils.*
import kotlin.system.exitProcess

class TestActivity : BasicActivity(), IAppLogicHandler {

    private lateinit var binding: ActivityTestBinding

    private var mIsExit: Boolean = false
    private var adapter: MyAdapter? = null


    override fun initialize(savedInstanceState: Bundle?) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_test)

        binding.recyclerView.layoutManager = LinearLayoutManager(this)

        adapter = MyAdapter()
        adapter?.setFragment(supportFragmentManager)

        binding.recyclerView.adapter = adapter

        binding.swipeRefreshLayout.setColorSchemeColors(resources.getColor(com.cmcc.passenger.R.color.passenger_colorAccents))
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.swipeRefreshLayout.isRefreshing = false
            initCard()
        }

        initCard()

        binding.login.clickWithTrigger {
//            SdkManager.setMyLinkToken("")
//            TokenUtils.setUserToken("")
//            initCard()
//            Toast.makeText(this, "已退出当前登录账号", Toast.LENGTH_LONG).show()

            ARouter.getInstance().build(PassPath.APP_LOGIN).navigation(
                this,
                1001
            )
        }

        SdkManager.init(this)
        onBroadcastReceiverClick(intent)
    }

    private fun initCard() {
        val token = SdkManager.getMyLinkToken()

        var list = mutableListOf<Card>()
        list.add(Card(isDriver = false))

        if (token == "13800000007" || token == "13800000008" || token == "13800000009"||token == "13800000010") {
            list.add(Card(isDriver = true))
        }
        adapter?.setList(list)
    }

    override fun initObserver(savedInstanceState: Bundle?) {

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        onBroadcastReceiverClick(intent)
    }

    private fun onBroadcastReceiverClick(intent: Intent?) {
        var bundle = intent?.extras
        if (bundle != null) {
            var messageType = bundle?.get("messageType")
            if (messageType != null) {
                val orderId = bundle?.get("orderId").toString()

                when (messageType.toString().toInt()) {
                    1 -> {//司机
                        val status = bundle?.get("status").toString().toInt()

                        ARouter.getInstance().build(ARouterPath.DRIVER_MAIN)
                            .withString("orderId", orderId)
                            .withInt("status", status)
                            .navigation()
                    }
                    2 -> {//乘客收到推送
                        val status = bundle?.get("status").toString().toInt()

                        ARouter.getInstance().build(ARouterPath.PASS_MAIN)
                            .withString("orderId", orderId)
                            .withInt("status", status)
                            .navigation()
                    }
                    5 -> {//司机休息

                    }
                }
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mIsExit) {
                SdkManager.setMyLinkToken("")
                exitProcess(0);
            } else {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mIsExit = true;

                Handler().postDelayed({
                    mIsExit = false;
                }, 2000)
            }
            return true;
        }
        return super.onKeyDown(keyCode, event)

    }

    override fun onPageStart(pageName: String?) {
        L.d("onPageStart----------------->$pageName")
    }

    override fun onPageEnd(pageName: String?) {
        L.d("onPageEnd----------------->$pageName")
    }

    override fun onEvent(context: Context?, key: String?, paramKey: String?, paramValue: String?) {

    }

    override fun tokenExpire() {
        SdkManager.setMyLinkToken("")
        ARouter.getInstance().build(PassPath.APP_LOGIN).navigation(
            this,
            1001
        )
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1001 && resultCode == Activity.RESULT_OK) {
            initCard()
        }
    }
}
